package com.sf.rbac.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sf.rbac.domain.Employee;
import com.sf.rbac.ex.BusinessException;
import com.sf.rbac.mapper.EmployeeMapper;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IEmployeeService;
import com.sf.rbac.vo.AdminVo;
import com.sf.rbac.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageInfo<Employee> query(QueryObject queryObject) {
        //告诉pageHelper    pageNum 和pageSize
        PageHelper.startPage(queryObject.getPageNum(), queryObject.getPageSize());
        // 调用分页函数selectForList() 查询数据
        List<Employee> employees = employeeMapper.selectForList(queryObject);
        return new PageInfo<Employee>(employees);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 根据id 删除用户数据
        int count = employeeMapper.delete(id);
        // 删除成功受影响行（row=1)  如果受影响行=0 ，抛出异常
        if (count == 0) {
            throw new BusinessException("非法操作");
        }
        // 删除用户和角色之间的关系  employee_role
        employeeMapper.deleteEmpAndRoleRelation(id);
    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.get(id);
        if (employee == null) {
            throw new BusinessException("非法操作");
        }
        return employee;
    }

    @Override
    public void saveOrUpdate(EmployeeVo employeeVo) {
        // 1 从vo 中获取employee 和roleIds
        Employee employee = employeeVo.getEmployee();
        Long[] roleIds = employeeVo.getRoleIds();
        //2判断id 是否为空
        if (employee.getId() == null) {
            //3 id=空
            // 3.1 属于新增操作
            // 3.2 新增用户信息
            employeeMapper.insert(employee);
        } else {
            //4 id！=空
            //4.1  属于编辑操作
            employeeMapper.update(employee);
            //4.2  编辑用户信息  删除旧关系
            employeeMapper.deleteEmpAndRoleRelation(employee.getId());
        }
        // 3.3 新增用户和角色之间的关系
        //4.3  更新用户和角色之间的关系
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertEmpAndRoleRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public void updateState(AdminVo adminVo) {
        // 非空校验
        if (adminVo.getId() == null) {
            throw new BusinessException("非法操作");
        }
        if (adminVo.getAdmin() == null) {
            throw new BusinessException("非法操作");
        }
        // 根据id  查询数据库信息
        Employee employee = employeeMapper.get(adminVo.getId());
        // 判断数据库中信息是否为空
        if (employee == null) {
            throw new BusinessException("非法操作");
        }
        //如果不为空更新管理员状态
        employeeMapper.updateState(adminVo);
    }


}
